﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using PPI.Client.Logic.Delegate;
using PPI.Entities.Interface;
using PPI.Entities.XML;

namespace PPI.Client.Logic.Upload
{
    public class UploadErrorEventArgs : EventArgs
    {
        private string m_ErrorMessage;

        public string ErrorMessage
        {
            get { return m_ErrorMessage; }
        }

        public UploadErrorEventArgs(string errorMessage)
            : base()
        {
            m_ErrorMessage = errorMessage;
        }
    }

    public class LUpload
    {
        private BackgroundWorker m_worker;

        public IUpload StrategyToUpload
        {
            set { m_StrategyToUpload = value; }
        }

        private IUpload m_StrategyToUpload;
        private UpdateBar m_UpdateBarDelegate;

        public LUpload(UpdateBar updateBarDelegate)
        {
            m_worker = new BackgroundWorker();
            m_worker.WorkerReportsProgress = true;
            m_worker.WorkerSupportsCancellation = true;
            m_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_worker_RunWorkerCompleted);
            m_worker.ProgressChanged += new ProgressChangedEventHandler(m_worker_ProgressChanged);
            m_worker.DoWork += new DoWorkEventHandler(m_worker_DoWork);
            m_UpdateBarDelegate = updateBarDelegate;
            m_StrategyToUpload = null;
        }

        public void Start()
        {
            if(!m_worker.IsBusy && m_StrategyToUpload != null)
                m_worker.RunWorkerAsync();
        }

        public void Stop()
        {
            if(m_worker.IsBusy)
            {
                m_worker.CancelAsync();
            }
        }

        public bool IsWorking()
        {
            return (m_worker.IsBusy);
        }

        void m_worker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_StrategyToUpload.StartWork(m_worker,e);
        }
        void m_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            m_UpdateBarDelegate(e.ProgressPercentage);

        }
        void m_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error == null && !e.Cancelled)
            {
                m_StrategyToUpload.FinishWork();
                OnFinishedUpload(this,new EventArgs());
                return;
            }

            if(e.Error != null)
            {
                OnUploadError(this, new UploadErrorEventArgs(e.Error.Message));
            }else
            {
                OnUploadCancel(this,new EventArgs());
            }
        }

        #region Events

        public event EventHandler<EventArgs> FinishedUpload;
        public event EventHandler<EventArgs> UploadCancel;
        public event EventHandler<UploadErrorEventArgs> UploadError;

        private void OnFinishedUpload(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (FinishedUpload != null)
            {
                FinishedUpload(o, e);
            }
        }
        private void OnUploadCancel(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (UploadCancel != null)
            {
                UploadCancel(o, e);
            }
        }
        private void OnUploadError(object o, UploadErrorEventArgs e)
        {
            // Safely invoke an event:
            if (UploadError != null)
            {
                UploadError(o, e);
            }
        }

        #endregion



    }
}
